package com.ruoyi.web.controller.qsng;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.AjaxResults;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.qsng.domain.KActivity;
import com.ruoyi.qsng.domain.KAgeGroup;
import com.ruoyi.qsng.service.IKActivityService;
import com.ruoyi.qsng.service.IKAgeGroupService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 活动信息Controller
 *
 * @author kanglou
 * @date 2023-06-07
 */
@Api(tags = "pc--活动信息Controller")
@RestController
@RequestMapping("/qsng/activity")
public class KActivityController extends BaseController {
    @Autowired
    private IKActivityService kActivityService;

    @Autowired
    private IKAgeGroupService kAgeGroupService;


    /**
     * 查询活动信息列表
     */
    @ApiOperation(value = "查询活动信息列表")
    @PreAuthorize("@ss.hasPermi('qsng:activity:list')")
    @GetMapping("/list")
    public TableDataInfo list(KActivity kActivity) {
        startPage();
        List<KActivity> list = kActivityService.selectKActivityList(kActivity);
        return getDataTable(list);
    }

    /**
     * 导出活动信息列表
     */
    @ApiOperation(value = "导出活动信息列表")
    @PreAuthorize("@ss.hasPermi('qsng:activity:export')")
    @Log(title = "活动信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, KActivity kActivity) {
        KAgeGroup kAgeGroup =new KAgeGroup();
        List<KAgeGroup> selectKAgeGroupList = kAgeGroupService.selectKAgeGroupList(kAgeGroup);

        List<KActivity> list = kActivityService.selectKActivityList(kActivity);
        for (KActivity kActivity1:list) {
            kActivity1.setRemainingQuota2(Math.toIntExact(kActivity1.getTotalQuota() - kActivity1.getSold()));
            //招生年龄
            for (KAgeGroup kAgeGroup1:selectKAgeGroupList) {
                if(kAgeGroup1.getId().equals(kActivity1.getAgeGroup()) ){
                    kActivity1.setAgeApp(kAgeGroup1.getName());
                    break;
                }
            }


        }
        ExcelUtil<KActivity> util = new ExcelUtil<KActivity>(KActivity.class);
        util.exportExcel(response, list, "活动信息数据");
    }

    /**
     * 获取活动信息详细信息
     */
    @ApiOperation(value = "获取活动信息详细信息")
    @PreAuthorize("@ss.hasPermi('qsng:activity:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id) {
        return success(kActivityService.selectKActivityById(id));
    }

    /**
     * 新增活动信息
     */
    @ApiOperation(value = "新增活动信息")
    @PreAuthorize("@ss.hasPermi('qsng:activity:add')")
    @Log(title = "活动信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResults add(@RequestBody KActivity kActivity) {
        if (kActivity.getBmKssj().compareTo(kActivity.getBmJssj()) > 0) {
            return AjaxResults.error("活动报名开始时间必须大于活动报名结束时间");
        }
        if (kActivity.getKkKksj().compareTo(kActivity.getKkJssj()) > 0) {
            return AjaxResults.error("活动开始时间必须大于活动结束时间");
        }
        if (kActivity.getBmJssj().compareTo(kActivity.getKkKksj()) > 0) {
            return AjaxResults.error("活动开始时间必须大于活动结束时间");
        }
        return AjaxResults.success(kActivityService.insertKActivity(kActivity));
    }

    /**
     * 修改活动信息
     */
    @ApiOperation(value = "修改活动信息")
    @PreAuthorize("@ss.hasPermi('qsng:activity:edit')")
    @Log(title = "活动信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResults edit(@RequestBody KActivity kActivity) {
        if (kActivity.getBmKssj().compareTo(kActivity.getBmJssj()) > 0) {
            return AjaxResults.error("活动报名开始时间必须大于活动报名结束时间");
        }
        if (kActivity.getKkKksj().compareTo(kActivity.getKkJssj()) > 0) {
            return AjaxResults.error("活动开始时间必须大于活动结束时间");
        }
        if (kActivity.getBmJssj().compareTo(kActivity.getKkKksj()) > 0) {
            return AjaxResults.error("活动开始时间必须大于活动结束时间");
        }
        return AjaxResults.success(kActivityService.updateKActivity(kActivity));
    }

    /**
     * 删除活动信息
     */
    @ApiOperation(value = "删除活动信息")
    @PreAuthorize("@ss.hasPermi('qsng:activity:remove')")
    @Log(title = "活动信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids) {
        return toAjax(kActivityService.deleteKActivityByIds(ids));
    }
}
